$(function () {
    let layer = layui.layer;
    let form = layui.form;
    let laypage = layui.laypage;
    let q = {
        pagenum: 1,
        pagesize: 2,
        cate_id: '',
        state: ''
    }
    template.defaults.imports.dateFormat = function (value) {
        let dt = new Date(value);
        let y = dt.getFullYear().padZero();
        let m = (dt.getMonth() + 1).padZero();
        let d = dt.getDate().padZero();
        let hh = dt.getHours().padZero();
        let mm = dt.getMinutes().padZero();
        let ss = dt.getSeconds().padZero();

        return `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
    }


    Number.prototype.padZero = function () {
        return String.prototype.padStart.call(this, 2, '0');
    }

    initTable();
    initCate();
    function initTable() {
        $.ajax({
            method: 'GET',
            url: '/my/article/list',
            data: q,
            success: function (res) {
                if (res.status !== 0) return layer.msg('获取文章信息失败');
                let htmlStr = template('tpl-table', res);
                $('tbody').html(htmlStr);
                renderPage(res.total);
            }

        })
    }


    function initCate() {
        $.ajax({
            method: 'GET',
            url: '/my/article/cates',
            data: q,
            success: function (res) {
                if (res.status !== 0) return layer.msg('获取分类失败');
                let htmlStr = template('tpl-cate', res);
                $('select[name=cate_id]').html(htmlStr);
                form.render();
            }
        })
    }

    $('#form-search').on('submit', function (e) {
        e.preventDefault();
        let cate_id = $('[name=cate_id]').val();
        let state = $('[name=state]').val();
        q.cate_id = cate_id;
        q.state = state;
        initTable();
    })


    function renderPage(total) {
        //console.log(total);

        laypage.render({
            elem: 'pageBox',
            count: total,
            limit: q.pagesize,
            curr: q.pagenum,
            layout: ['count', 'limit', 'prev', 'page', 'next', 'skip'],
            limits: [2, 3, 5, 10],
            jump: function (obj, first) {
                q.pagenum = obj.curr;
                q.pagesize = obj.limit;
                if (!first) {
                    initTable();
                }
            }
        })
    }



    $('tbody').on('click', '.btn-delete', function () {

        let id = $(this).attr('data-id');

        let len = $('.btn-delete').length;
        console.log(len);

        layer.confirm('确定删除吗?', { icon: 3, title: '提示' }, function (index) {
            //do something
            $.ajax({
                method: 'GET',
                url: '/my/article/delete/' + id,
                success: function (res) {
                    if (res.status !== 0) return layer.msg('删除文章失败');
                    layer.msg('删除文章成功');
                    if (len === 1) {
                        q.pagenum = q.pagenum === 1 ? 1 : q.pagenum - 1;
                    }

                    // if (len === 1 && q.pagenum>1) {
                    //      q.pagenum--;
                    // }
                    layer.close(index);
                    initTable();
                }
            })

        });
    })



})